package model;

import java.util.ArrayList;
import java.util.Date;

import model.type.SubjectAttributeType;
import model.type.SubjectType;
import db.ImbiConnection;

public class Enterprise{
	private final int id;
	private String name;
	private String fullName;
	private int createdBy;
	private int updatedBy;
	private Date created;
	private Date updated;
	
	public Enterprise(int id, String name, String fullName, int createdBy,
			int updatedBy, Date created, Date updated) {
		super();
		this.id = id;
		this.name = name;
		this.fullName = fullName;
		this.createdBy = createdBy;
		this.updatedBy = updatedBy;
		this.created = created;
		this.updated = updated;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public int getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(int createdBy) {
		this.createdBy = createdBy;
	}

	public int getUpdatedBy() {
		return updatedBy;
	}

	public void setUpdatedBy(int updatedBy) {
		this.updatedBy = updatedBy;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Date getUpdated() {
		return updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}

	public int getId() {
		return id;
	}
	
	/*OBJECT ACCESS METHODS*/
	
	public boolean isCustomer(){
		return ImbiConnection.getCustomerDAO().findByEnterpriseId(this).size() > 0;
	}	
	
	public ArrayList<Customer> getCustomer(){
		return ImbiConnection.getCustomerDAO().findByEnterpriseId(this);
	}	
	
	public void makeCustomer(){
		Customer c = new Customer(0, getId(), SubjectType.ENTERPRISE);
		ImbiConnection.getCustomerDAO().insert(c);
	}
	
	public Employee getCreaterEmployee(){
		return getEmployeeById(getCreatedBy());
	}
	
	public ArrayList<Employee> getEmployees(){
		return ImbiConnection.getEmployeeDAO().findAllByEnterprise(this);
	}
	
	public ArrayList<Contact> getContactList(){
		return ImbiConnection.getContactDAO().getContacts(this);
	}
	
	public ArrayList<EnterprisePersonRelation> getRelatedPersons(){
		return ImbiConnection.getEnterprisePersonRelationDAO().findAllByEnterprise(this);
	}
	
	public ArrayList<SubjectAttribute> getAttributes(){
		ArrayList<SubjectAttribute> attrList = ImbiConnection.getSubjectAttributeDAO().findByEnterpriseId(this);
		for(Customer c : getCustomer()){
			attrList.addAll(ImbiConnection.getSubjectAttributeDAO().findByCustomerId(c));
		}		
		return attrList;
	}
	
	public ArrayList<Address> getAddressList(){
		return ImbiConnection.getAddressDAO().findBySubjectId(getId(), SubjectType.ENTERPRISE);
	}
	
	public Employee getUpdaterEmployee(){
		return getEmployeeById(getUpdatedBy());
	}
	
	public ArrayList<SubjectAttributeType> getAvailableAttributes(){
		ArrayList<SubjectAttributeType> list = new ArrayList<SubjectAttributeType>();
		list.addAll(
				SubjectAttributeType.getAll(
					SubjectType.ENTERPRISE, 
					isCustomer()?SubjectType.CUSTOMER:0					
				)
			);		
		return list;
	}
	
	private Employee getEmployeeById(int id){
		return ImbiConnection.getEmployeeDAO().find(id);
	}
	
}
